package com.zhang.infrastructure.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 *   深圳金雅福控股集团有限公司
 *  @author zhangfanjun
 * @date 2021/11/17
 * @date 2022/4/7
 */
@EnableAsync
@Configuration
@Slf4j
public class AsyncThreadConfig implements AsyncConfigurer {

    /**
     * 线程管理
     * @author zfj
     * @date 2022/3/8
     */
    @Bean("taskExecutor")
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("alumni-");
        executor.initialize();
        return executor;
    }

    /**
     * 异常处理
     * @author zfj
     * @date 2022/3/8
     */
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new AlumniAsyncUncaughtExceptionHandler();
    }
}
